
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <include/pf_xml.h>

int main()
{
    pf_xml_t* myxml = NULL;
    pf_xml_t* parse_myxml = NULL;

    pf_xml_element_t* element = NULL;
    pf_xml_element_t* root = NULL;
    pf_xml_attribute_t* attr = NULL;
    char* outstr = NULL;
    int idx = 0;

    myxml = pf_xml_create("root");
    pf_xml_init_declare(myxml, "1.0", "ISO-8859-1", NULL);
    root = pf_xml_get_root(myxml);
    outstr = pf_xml_elem_get_text(root);
    if(outstr)
    {
        printf("get root 's text = %s\n", outstr);
    }
    else
    {
        printf("not get root 's text\n");
    }
    

    element = pf_xml_insert_element(root, "element1");
    pf_xml_insert_text(element, "element1 's text");
    pf_xml_insert_attribute(element, "aakey", "aavalue");
    pf_xml_insert_text(element, "element1 's text  222");
    pf_xml_insert_attribute(element, "bbkey", "bbvalue");
    element = pf_xml_insert_element(root, "element2");
    element = pf_xml_insert_element(root, "element3");
    pf_xml_insert_text(element, "element3 's text");
    outstr = pf_xml_elem_get_text(element);
    if(outstr)
    {
        printf("get element3 's text = %s\n", outstr);
    }
    else
    {
        printf("not get element3 's text\n");
    }
    
    outstr = pf_xml_elem_get_name(element);
    if(outstr)
    {
        printf("get element3 's name = %s\n", outstr);
    }
    else
    {
        printf("not get element3 's text\n");
    }
    

    outstr = pf_xml_dump(myxml, 1);
    if(outstr)
    {
        printf("dump xml format string= %s\n", outstr);
    }
    else
    {
        printf("not get get dump xml format string\n");
    }
    if(outstr)
    {
        free(outstr);
    }


    outstr = pf_xml_dump(myxml, 0);
    if(outstr)
    {
        printf("dump xml not format string= %s\n", outstr);
    }
    else
    {
        printf("not get get dump xml not format  string\n");
    }
    if(outstr)
    {
        free(outstr);
    }

    pf_xml_destroy(myxml);



    parse_myxml = pf_xml_parse("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
                                "<note>\n"
                                "<to>George</to>\n"
                                "<from>John</from>\n"
                                "<heading>Reminder</heading>\n"
                                "<body1>Don't forget the meeting!\n</body1>"
                                "<body2>Don't forget the meeting!</body2>\n"
                                "<body3 attr=\"123\" attr2=\'456\' attr2=\"789\">Don't forget the meeting!</body3>\n"
                                "</note>\n");

    if(parse_myxml == NULL)
    {
        printf("parse error!\n");
        return -1;
    }
    root = pf_xml_get_root(parse_myxml);
    pf_xml_insert_cdata(root,"aabbccdd");
    element = pf_xml_find_element(root, "body3");
    attr = pf_xml_find_attribute(element, "attr2");

    
    attr = pf_xml_enum_attributes(element, &idx);
    attr = pf_xml_enum_attributes(element, &idx);
    attr = pf_xml_enum_attributes(element, &idx);
    attr = pf_xml_enum_attributes(element, &idx);


    outstr = pf_xml_dump(parse_myxml, 1);
    if(outstr)
    {
        printf("dump another xml string= %s\n", outstr);
    }
    else
    {
        printf("not get get another dump xml string\n");
    }
    if(outstr)
    {
        free(outstr);
    }

    pf_xml_destroy(parse_myxml);

    return 0;
}
